﻿using System;
using UnityEngine;
#if UNITY_WX
using WeChatWASM;
#endif

namespace Core.Platform
{
    /// <summary>
    /// 微信小游戏
    /// </summary>
    public class WXWASMPlatform : Component, IAwake, IPlatform
    {
        public void Awake()
        {
#if UNITY_WX

            //wx.getSystemSetting;
            //wx.getAppAuthorizeSetting;
            //wx.getDeviceInfo;
            //wx.getWindowInfo;
            //wx.getAppBaseInfo;

            var deviceInfo = WX.GetDeviceInfo();

            log.d($"deviceInfo.abi = {deviceInfo.abi}");
            log.d($"deviceInfo.benchmarkLevel = {deviceInfo.benchmarkLevel}");
            log.d($"deviceInfo.brand = {deviceInfo.brand}");
            log.d($"deviceInfo.model = {deviceInfo.model}");
            log.d($"deviceInfo.platform = {deviceInfo.platform}");
            log.d($"deviceInfo.system = {deviceInfo.system}");


            var systemSetting = WX.GetSystemSetting();

            log.d($"systemSetting.bluetoothEnabled = {systemSetting.bluetoothEnabled}");
            log.d($"systemSetting.deviceOrientation = {systemSetting.deviceOrientation}");
            log.d($"systemSetting.locationEnabled = {systemSetting.locationEnabled}");
            log.d($"systemSetting.wifiEnabled = {systemSetting.wifiEnabled}");

            var appAuthorizeSetting = WX.GetAppAuthorizeSetting();

            log.d($"appAuthorizeSetting.albumAuthorized = {appAuthorizeSetting.albumAuthorized}");
            log.d($"appAuthorizeSetting.bluetoothAuthorized = {appAuthorizeSetting.bluetoothAuthorized}");
            log.d($"appAuthorizeSetting.cameraAuthorized = {appAuthorizeSetting.cameraAuthorized}");
            log.d($"appAuthorizeSetting.locationAuthorized = {appAuthorizeSetting.locationAuthorized}");
            log.d($"appAuthorizeSetting.locationReducedAccuracy = {appAuthorizeSetting.locationReducedAccuracy}");
            log.d($"appAuthorizeSetting.microphoneAuthorized = {appAuthorizeSetting.microphoneAuthorized}");
            log.d($"appAuthorizeSetting.notificationAlertAuthorized = {appAuthorizeSetting.notificationAlertAuthorized}");
            log.d($"appAuthorizeSetting.notificationAuthorized = {appAuthorizeSetting.notificationAuthorized}");
            log.d($"appAuthorizeSetting.notificationBadgeAuthorized = {appAuthorizeSetting.notificationBadgeAuthorized}");
            log.d($"appAuthorizeSetting.notificationSoundAuthorized = {appAuthorizeSetting.notificationSoundAuthorized}");
            log.d($"appAuthorizeSetting.phoneCalendarAuthorized = {appAuthorizeSetting.phoneCalendarAuthorized}");

            var appBaseInfo = WX.GetAppBaseInfo();

            log.d($"appBaseInfo.SDKVersion={appBaseInfo.SDKVersion}");
            log.d($"appBaseInfo.SDKVersion={appBaseInfo.enableDebug}");
            log.d($"appBaseInfo.SDKVersion={appBaseInfo.host}");
            log.d($"appBaseInfo.SDKVersion={appBaseInfo.language}");
            log.d($"appBaseInfo.SDKVersion={appBaseInfo.version}");
            log.d($"appBaseInfo.SDKVersion={appBaseInfo.theme}");
#endif
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();
        }

        public void Exit()
        {
#if UNITY_WX
            WX.ExitMiniProgram(new ExitMiniProgramOption() { complete = OnExitHandler });
            Application.Quit();
#endif
        }

#if UNITY_WX
        private void OnExitHandler(GeneralCallbackResult result)
        {
            log.err(result);
        }
#endif
    }
}
